package com.library.entity;

import javax.persistence.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * entiyu:借阅
 * Created by mhy on 2018/1/6.
 */
@Entity
@Table(name="borrow")
public class Borrow {
    private Integer borrowId;       //借阅记录ID
    private Staff staff;            //员工
    private Book book;              //图书
    private Administrator admin;     //最后处理的管理员
    private String startDate;       //借阅开始时间
    private String endDate;         //借阅结束时间
    private long allDays;        //此次借阅的总天数
    private long borrowDays;        //已经借了多少天
    private long endDays;         //还剩多少天可借
    private Integer state;           //借阅的状态 0表示未取书 1表示正在借阅中 2表示归还中 3表示已归还 4表示已超期 5表示丢失
    private Set<Evaluate> evaluates =new HashSet<Evaluate>();	//评论

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getBorrowId() {
        return borrowId;
    }

    public void setBorrowId(Integer borrowId) {
        this.borrowId = borrowId;
    }

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "staffId")
    public Staff getStaff() {
        return staff;
    }

    public void setStaff(Staff staff) {
        this.staff = staff;
    }

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "bookId")
    public Book getBook() {
        return book;
    }

    public void setBook(Book book) {
        this.book = book;
    }

    @ManyToOne(cascade = CascadeType.PERSIST)
    @JoinColumn(name = "adminId")
    public Administrator getAdmin() {
        return admin;
    }

    public void setAdmin(Administrator admin) {
        this.admin = admin;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }

    public Integer getState() {
        return state;
    }

    public void setState(Integer state) {
        this.state = state;
    }

    @Transient
    public long getBorrowDays() throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = getStartDate();
        String endDate;
        if(this.state == 3)
            endDate = this.getEndDate();
        else
            endDate = formatter.format(new Date());

        return getDays(endDate, startDate);
    }

    public void setBorrowDays(long borrowDays) {
        this.borrowDays = borrowDays;
    }

    @Transient
    public long getEndDays() throws ParseException {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String startDate = formatter.format(new Date());
        String endDate = getEndDate();

        return getDays(endDate, startDate);
    }

    public void setEndDays(long endDays) {
        this.endDays = endDays;
    }

    @Transient
    public long getAllDays() throws ParseException {
        String startDate = getStartDate();
        String endDate = getEndDate();

        return getDays(endDate, startDate);
    }

    public void setAllDays(long allDays) {
        this.allDays = allDays;
    }


    private long getDays(String date1, String date2) throws ParseException {
        Date s = new SimpleDateFormat("yyyy-MM-dd").parse(date1);
        Date e = new SimpleDateFormat("yyyy-MM-dd").parse(date2);

        return (s.getTime() - e.getTime()) / (24 * 60 * 60 * 1000);
    }

    @OneToMany(cascade=CascadeType.PERSIST,orphanRemoval = true)            //指定一对多关系
    @JoinColumn(name="borrowId")
    public Set<Evaluate> getEvaluates() {
        return evaluates;
    }

    public void setEvaluates(Set<Evaluate> evaluates) {
        this.evaluates = evaluates;
    }
}
